package com.song.article.controller;

import com.song.api.config.RabbitMQConfig;
import com.song.api.config.RabbitMQDelayConfig;
import com.song.article.stream.StreamService;
import com.song.grace.result.GraceJSONResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("producer")
public class RabbitMQController{

    private static final Logger LOG = LoggerFactory.getLogger(RabbitMQController.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private StreamService streamService;

    @RequestMapping("/stream")
    public Object streamProducer() {
        streamService.sendStream();
        return GraceJSONResult.ok();
    }


    @GetMapping("hello")
    public Object getHello() {

        /**
         * rabbitmq 的路由规则 routing key
         *     display.*.* --> * 代表1个占位符
         *         例:
         *         display.do.download     匹配
         *         display.do.download.failed  不匹配
         *
         *     display.# --> # 代表多个占位符
         *         例:
         *         display.do.download   匹配
         *         display.do.download.failed   匹配
         */
//        rabbitTemplate.convertAndSend(
//                RabbitMQConfig.EXCHANGE_ARTICLE,
//                "article.download",
//                "这是生产者的测试~");

//        rabbitTemplate.convertAndSend(
//                RabbitMQConfig.EXCHANGE_ARTICLE,
//                "article.download.do",
//                "100");

        rabbitTemplate.convertAndSend(
                RabbitMQConfig.EXCHANGE_ARTICLE,
                "article.download.html.do",
                "200");

        rabbitTemplate.convertAndSend(
                RabbitMQConfig.EXCHANGE_ARTICLE,
                "article.do",
                "300");

        rabbitTemplate.convertAndSend(
                RabbitMQConfig.EXCHANGE_ARTICLE,
                "article.html",
                "400");
        return GraceJSONResult.ok();
    }

    @GetMapping("/delay")
    public Object delay() {

        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                // 设置持久
                message.getMessageProperties()
                        .setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                // 设置延迟的时间，单位毫秒
                message.getMessageProperties().setDelay(5000);
                return message;
            }
        };

        // 发送延迟消息
        rabbitTemplate.convertAndSend(
                RabbitMQDelayConfig.EXCHANGE_DELAY,
                "delay.demo",
                "这是一条延迟消息~",
                messagePostProcessor);

        System.out.println("生产者发送延迟消息：" + new Date());

        return "OK";
    }


}
